package com.cennavi.minemap.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;
import com.cennavi.minemap.bean.UploadBean;

@Deprecated
@Component
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public class UploadDao extends AbstractDao {

	public int insert(final UploadBean uploadBean) {
		jdbcTemplate.setDataSource(pgDataSource);
		final String sql = "INSERT INTO MM_UPLOAD (name, userid, type, status, uploadTime, fileSize, describe) "
				+ "VALUES (?, ?, ?, 0, ?, ?, ?);";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbcTemplate.update(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
				PreparedStatement ps = conn.prepareStatement(sql,
						new String[] { "id" });
				ps.setString(1, uploadBean.getName());
				ps.setInt(2, uploadBean.getUserId());
				ps.setInt(3, uploadBean.getType());
				ps.setString(4, uploadBean.getUploadTime());
				ps.setLong(5, uploadBean.getFileSize());
				ps.setString(6, uploadBean.getDescribe());
				return ps;
			}
		}, keyHolder);
		return keyHolder.getKey().intValue();

	}

	public int update(final UploadBean uploadBean) {
		jdbcTemplate.setDataSource(pgDataSource);
		String setSql = super.getSetSql(uploadBean);
		String sql = "UPDATE MM_UPLOAD SET " + setSql + " WHERE id = ?";
		return jdbcTemplate.update(sql, new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setInt(1, uploadBean.getId());
			}
		});
	}
	
	public UploadBean queryById(int id){
		jdbcTemplate.setDataSource(pgDataSource);
		String sql = "SELECT * FROM MM_UPLOAD WHERE id = ?";
		return jdbcTemplate.queryForObject(sql, new Object[] { id }, new UploadMapper());
	}
	
	public List<UploadBean> queryByUserId(int userId){
		jdbcTemplate.setDataSource(pgDataSource);
		String sql = "SELECT * FROM MM_UPLOAD WHERE userId = ?";
		return (List<UploadBean>)jdbcTemplate.query(sql, new Object[] { userId }, new UploadMapper());
	}
	
	public int insertDetail(List<Map<String, Object>> list){
		jdbcTemplate.setDataSource(pgDataSource);
		String sql = "INSERT INTO MM_UPLOAD_DETAIL(userId, uploadId, attr, geom) VALUES (?, ?, ?, st_geomfromtext(?, 4326))";
        int[] result = jdbcTemplate.batchUpdate(sql, new UpdateDetailBatchPreparedStatementSetter(list));  
        return result.length;  
	}
	
	protected class UploadMapper implements RowMapper<UploadBean> {
		public UploadBean mapRow(ResultSet rs, int rowNum) throws SQLException {
			UploadBean upload = new UploadBean();
			upload.setId(rs.getInt("id"));
			upload.setName(rs.getString("name"));
			upload.setPath(rs.getString("path"));
			upload.setUserId(rs.getInt("userId"));
			upload.setStatus(rs.getInt("status"));
			upload.setType(rs.getInt("type"));
			upload.setUploadTime(rs.getString("uploadTime"));
			upload.setFileSize(rs.getLong("fileSize"));
			upload.setUploadSize(rs.getLong("uploadSize"));
			upload.setFileLine(rs.getInt("fileLine"));
			upload.setSuccLine(rs.getInt("succLine"));
			upload.setFailLine(rs.getInt("failLine"));
			upload.setDescribe(rs.getString("describe"));
			return upload;
		}
	}
	
	protected class UpdateDetailBatchPreparedStatementSetter implements BatchPreparedStatementSetter{  
	    final List<Map<String, Object>> list;  
	    /**通过构造函数把要插入的数据传递进来处理*/  
	    public UpdateDetailBatchPreparedStatementSetter(List<Map<String, Object>> list){  
	        this.list = list;  
	    }  
	    public int getBatchSize() {  
	        return list.size();  
	    }  
	  
	    public void setValues(PreparedStatement ps, int i)  
	            throws SQLException {  
	        Map<String, Object> map = (HashMap<String, Object>)list.get(i); 
	        ps.setInt(1, (Integer)map.get("userId"));  
	        ps.setInt(2, (Integer)map.get("uploadId"));  
	        ps.setString(3, (String)map.get("attr"));  
	        ps.setString(4, (String)map.get("geom"));
	    }  
	} 
	
}
